/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import JDBCUtil.MyConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.admin.Country;

/**
 *
 * @author Danh
 */
public class CountryManager {

    Connection conn;

    public CountryManager() {
        try {
            conn = MyConn.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Country> getCountry() {
        List<Country> list = new ArrayList<Country>();
        try {
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Countries");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Country c = new Country(rs.getInt("Country_Id"), rs.getNString("Country_Name"), rs.getBoolean("Country_Status"));
                list.add(c);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean insertCountry(Country c) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO Countries(Country_Status,Country_Name) VALUES(?,?)");
            pstmt.setBoolean(1, c.isCountryStatus());
            pstmt.setNString(2, c.getCountryName());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
        }
        return false;
    }

    public boolean update(Country c) {
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement("UPDATE Countries SET Country_Status=? ,Country_Name =? WHERE Country_Id=?");
            pstmt.setBoolean(1, c.isCountryStatus());
            pstmt.setNString(2, c.getCountryName());
            pstmt.setInt(3, c.getCountryId());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteCountry(int id) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("DELETE FROM Countries  WHERE Country_Id=?");
            pstmt.setInt(1, id);
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
        }

        return false;
    }
}
